package com.yijiankang.threaddemo.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;

@Slf4j
public class Synchronized1 {


    //修饰代码块   不同对象调用 互相不影响
    public void test1(int j){
        synchronized (this){
            for(int i=0;i<10;i++){
                log.info("test-1:{}--{}",i,j);
            }
        }
    }

    //修饰一个方法 作用域调用对象  不同对象调用互不影响
    public synchronized  void test2(int j){
        for(int i=0;i<10;i++){
            log.info("test-2:{}--{}",i,j);
        }
    }

    public static void main(String[] args) {
        Synchronized1 synchronized1 = new Synchronized1();
        Synchronized1 synchronized2 = new Synchronized1();
        ExecutorService executorService = Executors.newCachedThreadPool();
       /* executorService.execute(()->{
            synchronized1.test1(1);
        });

        executorService.execute(()->{
            synchronized2.test1(2);
        });*/

        executorService.execute(()->{
            synchronized1.test2(1);
        });

        executorService.execute(()->{
            synchronized2.test2(2);
        });
    }
}
